
if (SERVER) then

	AddCSLuaFile( "shared.lua" )
	SWEP.Weight				= 5
	SWEP.AutoSwitchTo		= false
	SWEP.AutoSwitchFrom		= false
	SWEP.HoldType			= "ar2"
	SWEP.ZoomMode = 0

end

if ( CLIENT ) then
	
	SWEP.ZoomMode = 0
	SWEP.ViewModelFOV		= 80
	
	SWEP.IconFont = "CSSelectIcons"
	
	SWEP.PrintName = "SIG SG-550"
	SWEP.Slot = 5
	SWEP.Slotpos = 3
	SWEP.IconLetter = "i"
	
	killicon.AddFont( "weapon_sg550_ar", "CSKillIcons", SWEP.IconLetter, Color( 255, 80, 0, 255 ) )

end

/*---------------------------------------------------------
   PRIMARY
   Semi Auto
---------------------------------------------------------*/

 SWEP.Base				= "tp_base"

 SWEP.ViewModel			= "models/weapons/v_snip_sg550.mdl"
 SWEP.WorldModel			= "models/weapons/w_snip_sg550.mdl"
 
 SWEP.ReloadDelay = nil

 SWEP.Primary.Sound			= Sound( "npc/sniper/echo1.wav" )
 SWEP.Primary.Sound2		= Sound( "weapons/sg552/sg552-1.wav" )
 SWEP.Primary.ZoomSound     = Sound( "weapons/g3sg1/g3sg1_slide.wav" )
 SWEP.Primary.Empty         = Sound( "weapons/ar2/ar2_empty.wav" )
 SWEP.Primary.Recoil			= 2.9
 SWEP.Primary.Damage			= 82
 SWEP.Primary.Cone				= 0.016
 SWEP.Primary.ClipSize          = 20
 SWEP.Primary.Delay				= .32
 SWEP.Primary.Automatic         = true
 SWEP.Primary.Ammo              = "ar2"

 SWEP.Secondary.Clipsize            = -1;
 SWEP.Secondary.DefaultClip         = -1;
 SWEP.Secondary.Automatic           = false
 SWEP.Secondary.Ammo                = "none"; 
 
function SWEP:Initialize()
	if SERVER then
		self:SetWeaponHoldType(self.HoldType)
	end
end

function SWEP:Reload()
	if (SERVER) then
		self.Owner:SetFOV( 90, .25 )
		self.ZoomMode = 0
		self.Weapon:SetNetworkedInt("ZoomMode", 0 )
	end
	if not self.ReloadDelay then
		self.Weapon:EmitSound(self.Primary.ZoomSound,100,120) 
	end
	self.ReloadDelay = self.ReloadDelay or CurTime() + .3
end

function SWEP:Deploy()
	self.Weapon:SendWeaponAnim(ACT_VM_DRAW)
	return true
end  

function SWEP:Holster()
	if (SERVER) then
		self.Owner:SetFOV( 90, .25 )
	end
	self.ZoomMode = 0
	self.Weapon:SetNetworkedInt("ZoomMode", 0 )
return true end

function SWEP:PrimaryAttack()

	if ( self.Weapon:Clip1() <= 0 ) then 
		if (SERVER) then
			self.Owner:SetFOV( 90, .25 )
		end
		self.ReloadDelay = CurTime() + .25
		self.Weapon:EmitSound(self.Primary.Empty,100,math.random(90,110)) 
		self.ZoomMode = 0
		self.Weapon:SetNetworkedInt("ZoomMode", 0 )
		self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
	return end
	
	if !self:CanPrimaryAttack() then return end
	self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
	
	self.Owner:ViewPunch(Angle(math.Rand(-0.2,-0.1) * self.Primary.Recoil, math.Rand(-0.1,0.1) *self.Primary.Recoil, 0))
	self:ShootBullets( self.Primary.Damage, 1, self.Primary.Cone)
	self.Weapon:EmitSound(self.Primary.Sound,100,math.random(70,80))
	self.Weapon:EmitSound(self.Primary.Sound2,100,math.random(98,102))
	self:TakePrimaryAmmo(1)
	self.Owner:ViewPunch(Angle(math.Rand(-0.2,-0.1) * self.Primary.Recoil, math.Rand(-0.1,0.1) *self.Primary.Recoil, 0))
	self.Weapon:SetRecoilTime(CurTime() + .5)
end

function SWEP:ShootBullets( damage, numbullets, aimcone)

	local tracer = 1
	if self.Weapon:GetNetworkedInt("ZoomMode") != 0 then
		tracer = 0
	end
	local scale = 1
	local canscale = true
	if (self.Owner:KeyDown(IN_DUCK) or self.Owner:KeyDown(IN_WALK)) and canscale then
		if self.Owner:KeyDown(IN_FORWARD) or self.Owner:KeyDown(IN_BACK) or self.Owner:KeyDown(IN_MOVELEFT) or self.Owner:KeyDown(IN_MOVERIGHT) then
			scale = math.Clamp(aimcone*0.06,0,10)
			canscale = false
		else
			scale = math.Clamp(aimcone*0.04,0,10)
			canscale = false
			if self.Owner:KeyDown(IN_WALK) and SERVER then
				self.Owner:TakeStamina(5)
			end
		end
	elseif (self.Owner:KeyDown(IN_SPEED) or self.Owner:KeyDown(IN_JUMP)) and canscale then
		scale = aimcone*1.5
		canscale = false
	elseif (self.Owner:KeyDown(IN_FORWARD) or self.Owner:KeyDown(IN_BACK) or self.Owner:KeyDown(IN_MOVELEFT) or self.Owner:KeyDown(IN_MOVERIGHT)) and canscale then
		scale = aimcone*1.1
	else
		scale = math.Clamp(aimcone*0.06,0,10)
	end
	
	local recoil = self.Weapon:GetRecoilRatio()
	scale = scale * recoil
	
	local bullet = {}
	bullet.Num 		= numbullets
	bullet.Src 		= self.Owner:GetShootPos()			
	bullet.Dir 		= self.Owner:GetAimVector()			
	bullet.Spread 	= Vector( scale, scale, 0 )		
	bullet.Tracer	= tracer
	bullet.Force	= math.Round(damage * 1.5)							
	bullet.Damage	= math.Round(damage * GameInfo.HumanDamageScale)
	bullet.AmmoType = "Pistol"
	bullet.TracerName 	= "Tracer"
	bullet.Callback = function ( attacker, tr, dmginfo )
		if tr.HitWorld or not tr.Entity then return end
		if not tr.Entity:IsPlayer() and not string.find(tr.Entity:GetClass(),"npc") then return end
		if tr.HitGroup == 1 and tr.Entity:IsValid() then
			local t2 = {}
			t2.start = tr.HitPos
			t2.endpos = self.Owner:GetAimVector() * 500
			t2.filter = tr.Entity
			local tr2 = util.TraceLine(t2)
			util.Decal("Blood", tr2.HitPos + tr2.HitNormal, tr2.HitPos - tr2.HitNormal)
			local effectdata = EffectData()
			effectdata:SetEntity(tr.Entity)
			effectdata:SetNormal(tr.HitNormal)
			effectdata:SetOrigin(tr.HitPos)
			util.Effect("headshot", effectdata)
			if math.random(1,3) == 1 then
				local effectdata = EffectData()
				effectdata:SetAngle(self.Owner:GetAimVector():Angle())
				effectdata:SetOrigin(tr.HitPos)
				util.Effect("head_gib", effectdata)
			end
			tr.Entity:EmitSound(ChooseRandom(GameSounds.HeadShot), 100, math.random(90,120))
		else
			util.Decal("Blood", tr.HitPos + tr.HitNormal, tr.HitPos - tr.HitNormal)
			local effectdata = EffectData()
			effectdata:SetOrigin(tr.HitPos)
			effectdata:SetNormal(tr.HitNormal)
			util.Effect("bodyshot", effectdata)
			local t2 = {}
			t2.start = tr.HitPos
			t2.endpos = self.Owner:GetAimVector() * 500
			t2.filter = tr.Entity
			local tr2 = util.TraceLine(t2)
			util.Decal("Blood", tr2.HitPos + tr2.HitNormal, tr2.HitPos - tr2.HitNormal)
		end
	end
	
	self.Owner:FireBullets( bullet )
	
	self.Weapon:SendWeaponAnim( ACT_VM_PRIMARYATTACK ) 		// View model animation
	self.Owner:MuzzleFlash()							// Crappy muzzle light
	self.Owner:SetAnimation( PLAYER_ATTACK1 )			// 3rd Person Animation
	
end

function SWEP:Think()
	if self.ZoomMode != 0 and SERVER then
		self.Owner:DrawViewModel( false )
	elseif SERVER then
		self.Owner:DrawViewModel( true )
	end
	if self.ReloadDelay then
		if self.ReloadDelay < CurTime() then
			self.ReloadDelay = nil
			self.Weapon:DefaultReload(ACT_VM_RELOAD)
		end
	end
end

SWEP.CrosshairScale = 1
function SWEP:DrawHUD()
	local mode = self.Weapon:GetNetworkedInt("ZoomMode")
	if mode != 0 then
		local w = ScrW()
		local h = ScrH()
		
		render.UpdateRefractTexture()
		
		surface.SetTexture(surface.GetTextureID("gmod/scope-refract"))
		surface.SetDrawColor(255, 255, 255, 255)
		surface.DrawTexturedRect(0, 0, w*1, h*1) 

		surface.SetTexture(surface.GetTextureID("gmod/scope"))
		surface.SetDrawColor(0, 0, 0, 255)
		surface.DrawTexturedRect(0, 0, w*1, h*1) 
		
	end
	
	local x = ScrW() * 0.5
	local y = ScrH() * 0.5
	local scalebywidth = (ScrW() / 1024) * 10

	local scale = 1
	local canscale = true
	if (self.Owner:KeyDown(IN_DUCK) or self.Owner:KeyDown(IN_WALK)) and canscale then
		if self.Owner:KeyDown(IN_FORWARD) or self.Owner:KeyDown(IN_BACK) or self.Owner:KeyDown(IN_MOVELEFT) or self.Owner:KeyDown(IN_MOVERIGHT) then
			scale = scalebywidth * self.Primary.Cone
			canscale = false
		else
			scale = scalebywidth * math.Clamp(self.Primary.Cone - (self.Primary.Cone/2),0,10)
			canscale = false
		end
	elseif (( self.Owner:KeyDown(IN_SPEED) and self.Owner:GetVelocity() != Vector(0,0,0) ) or self.Owner:KeyDown(IN_JUMP)) and canscale then
		scale = scalebywidth * self.Primary.Cone + (self.Primary.Cone*12)
		canscale = false
	elseif (self.Owner:KeyDown(IN_FORWARD) or self.Owner:KeyDown(IN_BACK) or self.Owner:KeyDown(IN_MOVELEFT) or self.Owner:KeyDown(IN_MOVERIGHT)) and canscale then
		scale = scalebywidth * self.Primary.Cone + (self.Primary.Cone*6)
	else
		scale = scalebywidth * self.Primary.Cone
	end
	
	local recoil = self.Weapon:GetRecoilRatio()
	scale = scale * recoil

	if self.Owner:KeyDown(IN_ZOOM) then
		scale = scale * 5
	end

	surface.SetDrawColor(220, 220, 220, 220)

	local dist = math.abs(self.CrosshairScale - scale)
	self.CrosshairScale = math.Approach(self.CrosshairScale, scale, FrameTime() * 2 + dist * 0.05)

	local gap = 40 * self.CrosshairScale
	local length = gap + 15 * self.CrosshairScale
	surface.DrawLine(x - length, y, x - gap, y)
	surface.DrawLine(x + length, y, x + gap, y)
	surface.DrawLine(x, y - length, x, y - gap)
	surface.DrawLine(x, y + length, x, y + gap)
	
end

function SWEP:SecondaryAttack()
	if CLIENT then return end
	self.ZoomMode = self.ZoomMode + 1
	if self.ZoomMode == 1 then	
		self.Weapon:SetNetworkedInt("ZoomMode", self.ZoomMode )
		self.Owner:SetFOV( 40, .5 )
		self.Owner:EmitSound(self.Primary.ZoomSound,100,120)
		self.Primary.Cone = 0.09
	elseif self.ZoomMode == 2 then
		self.Weapon:SetNetworkedInt("ZoomMode", self.ZoomMode )
		self.Owner:SetFOV( 18, .4 )
		self.Owner:EmitSound(self.Primary.ZoomSound,100,130)
		self.Primary.Cone = 0.05
	elseif self.ZoomMode == 3 then
		self.ZoomMode = 0
		self.Weapon:SetNetworkedInt("ZoomMode", self.ZoomMode )
		self.Owner:SetFOV( 0, .3 )
		self.Owner:EmitSound(self.Primary.ZoomSound,100,110)
		self.Primary.Cone = 1.2
	end
end
